package com.polaris.live.widget

import android.content.Context
import android.util.AttributeSet
import androidx.appcompat.widget.AppCompatTextView
import com.blankj.utilcode.util.ColorUtils
import com.polaris.live.utils.RichCharmImgUtil

/**
 *  根据等级变化颜色的text
 * @author Created by 天晴 on 2024/4/11 10:17
 * @since 1.0.0
 **/
class LevelChangeColorText(context: Context, attrs: AttributeSet?) :
    AppCompatTextView(context, attrs) {

    fun setLevel(
        userId: Long?,
        richLevel: Long?,
        vipLevel: Long?,
        @RichCharmImgUtil.TextColorType type: Int,
        defaultColorId: Int,
    ) {
        val defaultColor = ColorUtils.getColor(defaultColorId)
        // 优先处理 VIP 等级颜色
        vipLevel?.let { level ->
            RichCharmImgUtil.getVipColorByLevel(level)?.let { vipColor ->
                setTextColor(vipColor)
                return
            }
        }

        // 根据类型设置对应的富豪等级颜色
        val color = when (type) {
            RichCharmImgUtil.NAME -> {
                RichCharmImgUtil.getNameColorByRichLevel(userId, richLevel, defaultColor)
            }
            RichCharmImgUtil.MESSAGE -> {
                RichCharmImgUtil.getTextColorByRichLevel(userId, richLevel, defaultColor)
            }
            else -> defaultColor
        }

        setTextColor(color)

    }

    /**
     * 设置服务器返回的颜色
     */
    fun setNicknameColor(
        nicknameColor: String?,
        defaultColorId: Int,
    ) {
        val color = RichCharmImgUtil.serColor2HexColor(nicknameColor) ?: ColorUtils.getColor(defaultColorId)
        setTextColor(color)
    }
}